package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.Employee;
import com.atguigu.springcloud.entities.OrderInfo;
import com.atguigu.springcloud.request.SaveOrderInfoRequest;
import com.atguigu.springcloud.request.UpdateOrderInfoStatusByIdRequest;
import com.atguigu.springcloud.response.BaseResponse;
import com.atguigu.springcloud.service.OrderInfoService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * Created by luojie on 2018/9/7.
 */
@RestController("orderInfoController")
@Api(tags = "订单接口")
@CrossOrigin //跨域访问 swagger 要使用到
public class OrderInfoController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OrderInfoService orderInfoService;

    @ApiOperation("新增订单")
    @PostMapping(value = "/employee/save")
    @ApiResponses({@ApiResponse(code = 200, message = "保存成功"),
            @ApiResponse(code = 400, message = "错误的请求")})
    public BaseResponse save(@ApiParam @RequestBody @Validated SaveOrderInfoRequest request, HttpServletResponse response){
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");

        logger.info("保存员工 [uuid:{}, request:{}]", uuid, request);
        OrderInfo orderInfo = orderInfoService.save(request);
        logger.info("保存员工 ，成功[uuid:{}, employee:{}]", uuid, orderInfo);
        return successSaveBaseResponse();
    }

    @ApiOperation("修改订单状态")
    @PostMapping("/updateStatus")
    @ApiResponses({@ApiResponse(code = 990018, message = "订单不存在"), @ApiResponse(code = 200, message = "查询成功")})
    public
    BaseResponse updateStatus(@ApiParam @RequestBody @Validated UpdateOrderInfoStatusByIdRequest request) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        logger.info("修改订单状态 [uuid:{}, request:{}]", uuid, request);
        OrderInfo orderInfo= orderInfoService.findById(request.getId());
        if(orderInfo == null){
            return new BaseResponse(990018, "订单不存在");
        }
        orderInfo = orderInfoService.updateStatus(orderInfo, request.getStatus());
        logger.info("修改订单状态，成功[uuid:{}, orderInfo:{}]", uuid, orderInfo);
        return successQueryOneResponse(orderInfo);
    }

    @ApiOperation(value = "查询用户订单")
    @GetMapping(value = "listByEmployeeId/{employeeId}")
    public @ResponseBody
    BaseResponse listByAreaInfoId(@ApiParam @PathVariable("employeeId") String employeeId, HttpServletResponse response) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        logger.info("查询用户订单[uuid:{}, employeeId:{}]", uuid, employeeId);

        List<OrderInfo> parkInfoEOS = orderInfoService.findByEmployeeId(employeeId);

        logger.info("查询用户订单:成功[uuid:{}, employeeId:{}]", uuid, employeeId);
        return successQueryMultiResponse(parkInfoEOS);
    }

    @ApiOperation("根据id查找员工")
    @GetMapping("/employee/get/{id}")
    public Employee get(@PathVariable("id") String id){

        return orderInfoService.findEmployeeById(id);
    }
}
